/**
 * Created by Administrator on 2017/7/20.
 */
(function() {
    "use strict";
    var app = angular.module("angularApp");
    app.controller('majorCtrl', function ($scope, $http,$uibModal,fac) {
        $scope.search = {year:2019,batch:1};
        $scope.yearDict = [[2017,2017],[2018,2018],[2019,2019]];
        $scope.batchDict = [[1,1],[2,2],[3,3]];
        $scope.pageModel = {};
        $scope.find = function(pageNo){
            $.extend($scope.search,{currentPage:pageNo||$scope.pageModel.currentPage||1,pageSize:$scope.pageModel.pageSize||10});
            fac.getPageResult("../major/list",$scope.search,function(data){
                $scope.pageModel = data;
            });
        };
        $scope.find();

        $scope.uploadMajor = function(){
            var modal = $uibModal.open({
                animation: false,
                size:'',
                templateUrl: 'view/school/major.upload.html',
                controller: 'uploadMajorCtrl'
                //  ,resolve: {item: angular.extend({},style,{'sceneId':sceneId})}
            });
            modal.result.then(function () {
                $scope.find();
                //$rootScope.elementEnum && $rootScope.elementEnum.forEach(function(n){ n.pageModel && delete n.pageModel.data});
            }, function () {
                console.info('Modal dismissed at: ' + new Date());
            });
        };

        $scope.uploadMajorResult = function(){
            var modal = $uibModal.open({
                animation: false,
                size:'',
                templateUrl: 'view/school/major_result.upload.html',
                controller: 'uploadMajorResultCtrl'
                //  ,resolve: {item: angular.extend({},style,{'sceneId':sceneId})}
            });
            modal.result.then(function () {
                $scope.find();
                //$rootScope.elementEnum && $rootScope.elementEnum.forEach(function(n){ n.pageModel && delete n.pageModel.data});
            }, function () {
                console.info('Modal dismissed at: ' + new Date());
            });
        };


    });



    app.controller('uploadMajorCtrl', function($scope,$http,$uibModalInstance,fac) {
        $scope.item = {year:2019};

        $scope.save = function (form) {
            form.$setSubmitted(true);
            if (!form.$valid) {
                return;
            }
            if(!$("#xmlInput").val()){
                alert("请选择上传excel！")
                return false;
            }
            var index = layer.load(1, {
                shade: [0.2, '#000'] //0.1透明度的白色背景
            });
            $("iframe[name='iframe_0']").on("load",function() {
                layer.close(index);
                var contents = $(this).contents().get(0);
                var data = $(contents).find('body').text();
                try{
                    data = window.eval('(' + data + ')');
                }catch(e){
                    console.log(data)
                    if(data ==""){
                        data = "请重新登录!";
                    }
                    data = {code:403,msg:data}
                }
                if (data.code === 0) {
                    $uibModalInstance.close();
                    confirm("成功上传"+data.data.inserted+"条记录！忽略了"+data.data.ignored+"条记录！");
                } else if(data.code === 2){
                    var message ="";
                    data.data.forEach(function(n){
                        message +="<p>"+n+"</p>";
                    })
                    layer.confirm(message, { btn: ['确定'], title: data.msg }, function (index) {
                        layer.close(index);
                    }, function () {
                    });

                } else{
                    alert(data.msg);
                }

            });
            $("form[name='form']").submit();

        }
    });


    app.controller('uploadMajorResultCtrl', function($scope,$http,$uibModalInstance,fac) {
        $scope.item = {year:2019};

        $scope.save = function (form) {
            form.$setSubmitted(true);
            if (!form.$valid) {
                return;
            }
            if(!$("#xmlInput").val()){
                alert("请选择上传excel！")
                return false;
            }
            var index = layer.load(1, {
                shade: [0.2, '#000'] //0.1透明度的白色背景
            });
            $("iframe[name='iframe_0']").on("load",function() {
                layer.close(index);
                var contents = $(this).contents().get(0);
                var data = $(contents).find('body').text();
                try{
                    data = window.eval('(' + data + ')');
                }catch(e){
                    console.log(data)
                    if(data ==""){
                        data = "请重新登录!";
                    }
                    data = {code:403,msg:data}
                }
                if (data.code === 0) {
                    $uibModalInstance.close();
                    confirm("导入专业:"+data.data.inserted+"条！更新了专业结果"+data.data.updated+"条！");
                } else if(data.code === 2){
                    var message ="";
                    data.data.forEach(function(n){
                        message +="<p>"+n+"</p>";
                    })
                    layer.confirm(message, { btn: ['确定'], title: data.msg }, function (index) {
                        layer.close(index);
                    }, function () {
                    });

                } else{
                    alert(data.msg);
                }

            });
            $("form[name='form']").submit();

        }
    });

})();